package com.zeddy.android.HarrisVoice;
/*
 * @Filename: 	HarrisMessage.java
 * @Classname: 	HarrisMessage
 * @Author:		i.z.chirombe@wustl.edu
 * 
 * @Description:	This is just a message class to facilitate message passing, between
 * 				all Harris components that seeks to do Robot/Human interaction 
 * 				activities. Although a message type is required for every message that 
 * 				exists, how that message looks like or the nature of its building 
 * 				blocks are not part of this message. This message class remains a generic 
 * 				message, all message construct are left to the application layer. But, the
 * 				order or nature and type of the first 3 fields should be strictly preserved.
 * 				eg. msg_type:username:msg_id:[ data goes here ]
 * 
 * 				This library class has its own direct port, that is used by PC based C++ clients.
 * 				The only minor difference is around the enumeration values are treated 
 * 				between java and C++.
 */
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * @author zeddy.chirombe
 *
 */

public class HarrisMessage {
	 public enum HarrisMessageTypes {
		 //--------All android here -----------
		 ANDROID_UNDEFINED,
		 ANDROID_INTRO,			// Just a welcome aboard message -
		 ANDROID_SUCCESS,
		 ANDROID_BROADCAST, 	// one way voice message - 
		 ANDROID_QUESTION, 		// Question with replay - 
		 ANDROID_GRID_LOCATION,	// Ask human for a desired map location - 
		 ANDROID_BIOMETRICS,	// ask Android to generate biometric signature -
		 ANDROID_DELETE_FACES   // delete used faces - 
		 //--------All ROS here ------------- 
	};
	public final String seperator = ":";
	public final String eof_msg_marker = "#";
	public String data = "";
	public HarrisMessageTypes MsgType;
	public int MsgId = 0;
	public ArrayList<String> FieldList = null;
	
	public HarrisMessage() {
		this.MsgType = HarrisMessageTypes.ANDROID_BROADCAST;
		FieldList = new ArrayList<String>();
	}
	public HarrisMessage(HarrisMessageTypes msgType) {
		this.MsgType = msgType;
	}
	public void reset() {
		FieldList.clear();
		MsgType = HarrisMessageTypes.ANDROID_UNDEFINED;
	}
	public String entry(int index) {
		return this.FieldList.get(index);
	}
	public  void add_entry(String field) {
		this.FieldList.add(field);
	}
	public String str() {
		this.data = "" + MsgType;
		for(int i = 0; i < FieldList.size(); i++) {
			this.data += seperator+FieldList.get(i);
		}
		return this.data;
	}
	public boolean parse_list(String list_str) {
		StringTokenizer st = null;
		MsgType = HarrisMessageTypes.ANDROID_UNDEFINED;
		try {
			FieldList.clear();
			st = new StringTokenizer(list_str,seperator);
			// extract msg_type - 
			if(st.hasMoreTokens()) {
				this.MsgType = HarrisMessageTypes.valueOf(st.nextToken());
				//Log.e("TCP","InMsgType[ "+MsgType+" ] ---");
				while(st.hasMoreTokens()) {
					this.FieldList.add(st.nextToken());
				}
				st = null;
				//Log.e("TCP","inMsg - "+str());
				if(FieldList.size() > 1) return true;
			} 
		} catch (UnsupportedOperationException e) {
			//System.out.println("Error::parse_list():");
		} catch (Exception e) {
			//System.out.println("G-Error::parse_list():");
		}
		st = null;
		return false;
	}
	
}